package app.base.common;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;

import sun.misc.BASE64Encoder;

public class Utility {
	public static final String TIME_ZONE_ID = "GMT-8:00"; 

	public static final String CHARSET_ENCODING_UTF8 = "UTF-8";
	public static final String CHARSET_ENCODING_GB2312 = "GB2312";
	public static final String DEFAULT_CHARSET_ENCODING = Utility.CHARSET_ENCODING_UTF8;

	// Carriage Return 13 \r
	// New Line 10 \n
	public static final String UNIX_TERMINATORS = "\n";
	public static final String DOS_TERMINATORS = "\r\n";
	public static final String MAC_TERMINATORS = "\r";
	
	public static final String DEFAULT_CONTENT_TYPE = "text/html;charset=UTF-8";
	
	public static final String DEFAULT_WEBAPP_CONTEXT_ROOT_REALPATH = null;

	public static final String DEFAULT_SPRINGFRAMEWORK_APPLICATION_CONTEXT_FILE_PATH = "applicationContext.xml";

	
	
	public static final String SYSTEM_MENUS_MANAGE_ROOTNODE_ID = "SYSMENUIDROOTNODE";
	public static final String SYSTEM_ROLES_MANAGE_ROOTNODE_ID = "SYSROLESIDROOTNODE";
	public static final String SYSTEM_PERMS_MANAGE_ROOTNODE_ID = "SYSPERMSIDROOTNODE";
	
	public static final String SYSTEM_MENUS_MANAGE_MODULE_BEAN_NAME = "sysMenusDAO";
	public static final String SYSTEM_MODULES_MANAGE_MODULE_BEAN_NAME = "sysModulesDAO";
	public static final String SYSTEM_PERMISSIONS_MANAGE_MODULE_BEAN_NAME = "sysPermissionsDAO";
	public static final String SYSTEM_ROLES_MANAGE_MODULE_BEAN_NAME = "sysRolesDAO";
	public static final String SYSTEM_ROLES_PERMS_MAPPING_MANAGE_MODULE_BEAN_NAME = "sysRolesPermsMappingDAO";
	
	public static final String SYSTEM_USER_TYPE_MANAGE_MODULE_BEAN_NAME = "sysUserTypeDAO";
	public static final String SYSTEM_USERS_MANAGE_MODULE_BEAN_NAME = "sysUsersDAO";
	public static final String SYSTEM_USERS_ROLES_MAPPING_MANAGE_MODULE_BEAN_NAME = "sysUsersRolesMappingDAO";

	public static final String ORGAN_MANAGE_MODULE_BEAN_NAME = "organInfoDAO";
	public static final String OPERATOR_MANAGE_MODULE_BEAN_NAME = "operatorInfoDAO";

	
	public static final String SYS_OPERATION_COMMAND_ADD = "1";
	public static final String SYS_OPERATION_COMMAND_MODIFY = "2";
	public static final String SYS_OPERATION_COMMAND_DELETE = "3";
	public static final String SYS_OPERATION_COMMAND_GENERATE = "4";
	public static final String SYS_OPERATION_COMMAND_QUERY = "5";
	public static final String SYS_OPERATION_COMMAND_LOGIN_VERIFY = "6";
	public static final String SYS_OPERATION_COMMAND_LOGIN_OUT = "7";
		
	public static final String LOGIN_USER_SESSION_INFO = "LOGIN_USER_INFO";

	
	public static final String APP_SYS_MODULES_CONFIGURATION_INCLUDE_PATH = "js/common/common.base.all.inc.js";
	public static final String APP_SYS_USER_LOGIN_INCLUDE_PATH = "js/SystemMainFrame/login.do.inc.js";

	public static final String SYSTEM_LOGO_TITLE = "后台管理系统V1.0";

	
	public static final String SPLIT_CHARS_VERTICAL_LINE = "|";

	public static final int START_FROM_MONDAY_FLAGS = 1;
	public static final int START_FROM_SATURDAY_FLAGS = 6;
	public static final int START_FROM_SUNDAY_FLAGS = 7;
	public static final int DAYS_OF_WEEK = 7;

	//获取一个月的总天数
	public static int getDaysOfMonth(int year, int month) 
	{   
		java.util.Calendar calendar = java.util.Calendar.getInstance(); 
		calendar.setTimeZone(java.util.TimeZone.getTimeZone(Utility.TIME_ZONE_ID));
		calendar.clear(); 
		calendar.set(java.util.Calendar.YEAR, year); 
		calendar.set(java.util.Calendar.MONTH, month - 1);          
		return calendar.getActualMaximum(java.util.Calendar.DAY_OF_MONTH);
	}
	
	//获取一个月的第day号是星期几：1 星期日, 2 星期一
	public static int getDayOfWeek(int year, int month, int day) 
	{   
		java.util.Calendar calendar = java.util.Calendar.getInstance();
		calendar.setTimeZone(java.util.TimeZone.getTimeZone(Utility.TIME_ZONE_ID));
		calendar.clear(); 
		calendar.set(java.util.Calendar.YEAR, year);   
		calendar.set(java.util.Calendar.MONTH, month - 1);   
		calendar.set(java.util.Calendar.DATE, day);   
		//java.text.DateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd   HH:MM:ss");   
		return calendar.get(java.util.Calendar.DAY_OF_WEEK);
	 } 

	public static ArrayList<String> generateDaysOfMonth(int year, int month, int format) 
	{
		month = month < 1 ? 1 : month;
		month = month > 12 ? 12 : month;
		
		int days = getDaysOfMonth(year, month);
		int day = getDayOfWeek(year, month, 1);
		
		switch (format)
		{
			case START_FROM_MONDAY_FLAGS:
				if (day == 1)
				{
					day += DAYS_OF_WEEK;
				}
				
				day -= 1;	//星期一从1开始
				break;
			case START_FROM_SATURDAY_FLAGS:
				day += 1;
				break;
			case START_FROM_SUNDAY_FLAGS:
				break;
			default:
				break;
		}
			
		//处理size = Utility.DAYS_OF_WEEK 的整数倍
		int size = days + (day - 1);
			
		if (!(size % DAYS_OF_WEEK == 0))
		{
			size += DAYS_OF_WEEK - size % DAYS_OF_WEEK;
		}
			
		//System.out.println("size is " + size);
		
		ArrayList<String> al = new ArrayList<String>();
		
		while(size-- > 0){
			al.add("");
		}
		
		day -= 1;	//从ArrayList<String> al 的index 0开始展开

		for (int index = 0; index < days; index++)
		{
			String strMonth = String.valueOf(month);
			String strDay = String.valueOf(index + 1);
			
			al.set(
					index + day, 
					String.valueOf(year) 
					+ "-" + (strMonth.length() > 1 ?  strMonth : "0" + strMonth)
					+ "-" + (strDay.length() > 1 ?  strDay : "0" + strDay)
			);
		}
				
		return al;
	}

	public static String MD5(String message) 
		throws NoSuchAlgorithmException, UnsupportedEncodingException
	{
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        BASE64Encoder base64en = new BASE64Encoder();

        return base64en.encode(md5.digest(message.getBytes(CHARSET_ENCODING_UTF8)));		
	}
	
	/**
	 * @param args
	 * @throws UnsupportedEncodingException 
	 * @throws NoSuchAlgorithmException 
	 */
	public static void main(String[] args) 
		throws NoSuchAlgorithmException, UnsupportedEncodingException 
	{
		// TODO Auto-generated method stub

	}
	
}
